﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Daidalos.VFS
{
    public class TextFile : File
    {
        /// <summary>
        /// 
        /// </summary>
        private System.IO.StreamReader read;

        /// <summary>
        /// 
        /// </summary>
        private System.IO.StreamWriter write;

        /// <summary>
        /// 
        /// </summary>
        private OpenMode openMode;

        /// <summary>
        /// 
        /// </summary>
        public OpenMode OpenMode
        {
            get
            {
                return openMode;
            }
            set
            {
                openMode = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="openmode"></param>
        /// <param name="filemode"></param>
        public TextFile(string name, OpenMode openmode)
        {
            if (openmode == OpenMode.READ)
            {
                read = new System.IO.StreamReader(name);
            }
            else if (openmode == OpenMode.WRITE)
            {
                write = new System.IO.StreamWriter(name);
            }
            else if (openmode == OpenMode.HYBRID)
            {
                read = new System.IO.StreamReader(name);
                write = new System.IO.StreamWriter(name);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Close()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string ReadLine()
        {
            if (OpenMode == OpenMode.WRITE || OpenMode == OpenMode.APPEND)
                throw new Exception("File not opened in read or hybrid mode!");

            return read.ReadLine();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Write(string text)
        {
            if (OpenMode == OpenMode.READ)
            {
                throw new Exception("File not opened in write or append mode!");
            }

            write.WriteLine(text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void WriteLine(string text)
        {
            Write(text + "\n");
        }

        /// <summary>
        /// 
        /// </summary>
        public void Flush()
        {
            if (OpenMode == OpenMode.READ)
            {
                throw new Exception("File not opened in write or append mode!");
            }
            write.Flush();
        }
    }
}
